<?php

class Test extends Model_Abstract
{
	public function getJulyUsers()
	{
		try
		{
			$sql = 'select id, gender, year(now())-year(birthday) as age, zipcode, pub, pla, created, last_visit, site_id from users where created>=1309492800 and created<1312171200';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getAug1Users()
	{
		try
		{
			$sql = 'select id, gender, year(now())-year(birthday) as age, zipcode, pub, pla, created, last_visit, site_id from users where created>=1312171200 and created<1313380800';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getAug2Users()
	{
		try
		{
			$sql = 'select id, gender, year(now())-year(birthday) as age, zipcode, pub, pla, created, last_visit, site_id from users where created>=1313380800 and created<1314849600';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getSep1Users()
	{
		try
		{
			$sql = 'select id, gender, year(now())-year(birthday) as age, zipcode, pub,pla, created, last_visit, site_id from users where created>=1314849600 and created<1316059200';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getSep2Users()
	{
		try
		{
			$sql = 'select id, gender, year(now())-year(birthday) as age, zipcode, pub, pla, created, last_visit, site_id from users where created>=1316059200 and created<1317441600';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}

	public function getTokens($id)
	{
		try
		{
			$sql = 'select sum(credit) as token from rewards where user_id=?';
			$result = $this->_connection->query($sql, array($id));
			if (count($result) > 0)
			{
				return empty($result[0]['token'])?0:$result[0]['token'];
			}
			else
			{
				return 0;
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}

	public function getFirstToken($id)
	{
		try
		{
			$sql = 'select created from rewards where user_id=? order by created limit 1';
			$result = $this->_connection->query($sql, array($id));
			if (count($result) > 0)
			{
				return date('Y-m-d', $result[0]['created']);
			}
			else
			{
				return 'N/A';
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
}
?>